package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.ProfileCriteria;
import com.leo.ibatis.demo.domain.Profile;
import com.leo.ibatis.demo.service.ProfileService;

public class ProfileController extends MultiActionController {

	private ProfileService profileService;

	public void setProfileService(ProfileService profileService) {
		this.profileService = profileService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ProfileCriteria profileCriteria = new ProfileCriteria();
		bind(request, profileCriteria);
		
		List<Profile> profileList = profileService.listOnPage(profileCriteria);
		
		ModelAndView mav = new ModelAndView("profile/list");
		mav.addObject("profileList", profileList);
		mav.addObject("profileCriteria", profileCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ProfileCriteria profileCriteria = new ProfileCriteria();
		bind(request, profileCriteria);
			
		String userid = ServletRequestUtils.getRequiredStringParameter(request, "userid");

		Profile profile = profileService.select(userid);
		
		ModelAndView mav = new ModelAndView("profile/detail");
		mav.addObject("profile", profile);
		mav.addObject("profileCriteria", profileCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userid = ServletRequestUtils.getRequiredStringParameter(request, "userid");

		profileService.delete(userid);
		
		return new ModelAndView("redirect:profile.do");
	}

}
